package cn.dankal.flyaudio.home

import api.CurrentServiceFactory
import cn.dankal.basiclib.base.callback.DKCallBack
import cn.dankal.basiclib.base.fragment.BaseRecyclerViewFragment
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewPresenter
import cn.dankal.basiclib.rx.AbstractDialogSubscriber
import cn.dankal.basiclib.rx.AbstractStateSubscriber
import cn.dankal.basiclib.util.ToastUtils
import cn.dankal.business.pojo.BaseBeanList
import cn.dankal.business.pojo.CurrentBean
import cn.dankal.business.pojo.ReasonBean
import io.reactivex.disposables.Disposable

class MyCurrentPrsenter : BaseRecyclerViewPresenter<CurrentBean>() {

    override fun requestData(pageIndex: String?) {
        CurrentServiceFactory.user_info_list(pageIndex, BaseRecyclerViewFragment.PAGE_SIZE)
                .subscribe(object :
                        AbstractStateSubscriber<BaseBeanList<CurrentBean>>(view) {
                    override fun onNext(t: BaseBeanList<CurrentBean>) {
                        super.onNext(t)
                        this@MyCurrentPrsenter.view.render(t.list)
                    }
                })
    }

    fun praise(uuid: String, callback: DKCallBack) {

        CurrentServiceFactory
                .praise_user_info("1", uuid, true)
                .subscribe(object : AbstractDialogSubscriber<ReasonBean>(view) {

                    override fun onNext(reasonBean: ReasonBean) {
                        callback.action()
                    }
                })
    }

    fun praise(uuid: String, position: Int, isPraise: Boolean, callback: CurrentPrsenter.PraiseCallback) {

        CurrentServiceFactory
                .praise_user_info("1", uuid, isPraise)
                .subscribe(object : AbstractDialogSubscriber<ReasonBean>(view) {

                    override fun onNext(reasonBean: ReasonBean) {
                        ToastUtils.showShort(reasonBean.message)
                        callback.onPraiseCallback(position)
                    }

                    override fun onSubscribe(d: Disposable) {

                    }
                })
    }

    fun deleteInfo(uuid: String,position: Int, callback: CurrentPrsenter.PraiseCallback) {

        CurrentServiceFactory
                .delete_info(uuid)
                .subscribe(object : AbstractDialogSubscriber<ReasonBean>(view) {

                    override fun onNext(reasonBean: ReasonBean) {
                        callback.onDetectCallback(reasonBean.message,position)
                    }
                })
    }

}